import socket
import os

obj = socket.socket()

ip_port = ("127.0.0.1", 8080)
obj.connect(ip_port)

ret = obj.recv(1024).decode("utf-8")
print(ret)

size = os.stat("./img/yan.jpg").st_size
# os.stat()是用来获取指定路径的状态信息，这里的指定路径可以是文件，也可以是文件夹。本质上，os.stat()就是执行系统调用stat

obj.sendall(bytes(str(size), encoding="utf-8"))

mid_ret = obj.recv(1024).decode("utf-8")
print(mid_ret)

with open("./img/yan.jpg", "rb") as f:
    for line in f:
        obj.sendall(line)

print(obj.recv(1024).decode("utf-8"))